package com.sqfw.project.wpReport.service.impl;


import com.github.pagehelper.PageInfo;
import com.sqfw.common.constant.HttpStatus;
import com.sqfw.framework.web.page.TableDataInfo;
import com.sqfw.project.system.domain.SysUser;
import com.sqfw.project.system.mapper.SysDeptMapper;
import com.sqfw.project.system.mapper.SysUserMapper;
import com.sqfw.project.wp.domain.WpVisitInfo;
import com.sqfw.project.wp.mapper.WpVisitInfoMapper;
import com.sqfw.project.wpReport.domain.VisitWorkReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sqfw.project.wpReport.service.IVisitWorkReportService;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 回访组工作情况汇总表Service业务层处理
 * 
 * @author lx
 * @date 2023-07-28
 */
@Service
public class VisitWorkReportServiceImpl implements IVisitWorkReportService 
{

    @Autowired
    private WpVisitInfoMapper wpVisitInfoMapper;

    @Autowired
    private SysUserMapper sysUserMapper;
    //回访组部门id
    private static final Long deptId = 107L;

    @Override
    public TableDataInfo getVisitWorkReportList(VisitWorkReport visitWorkReport) {

        List<VisitWorkReport> visitWorkReports = new ArrayList<>();

        SysUser sysUser = new SysUser();

        sysUser.setDeptId(deptId);
        sysUser.setCode(visitWorkReport.getCode());
        sysUser.setNickName(visitWorkReport.getNickName());

        List<SysUser> sysUsers = sysUserMapper.selectUserByDeptIdAndCode(sysUser);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(new PageInfo(sysUsers).getTotal());
        //未查询到对应坐席人员,直接返回
        if (sysUsers.size() == 0) {
            tableDataInfo.setRows(visitWorkReports);
            tableDataInfo.setMsg("查询成功");
            tableDataInfo.setCode(HttpStatus.SUCCESS);
            return tableDataInfo;
        }

        WpVisitInfo wpVisitInfo = new WpVisitInfo();

        wpVisitInfo.setBegainTime(visitWorkReport.getBegainTime());
        wpVisitInfo.setEndTime(visitWorkReport.getEndTime());
        for (SysUser user : sysUsers) {
            VisitWorkReport visitWorkReportCon = new VisitWorkReport();

            visitWorkReportCon.setNickName(user.getNickName());
            visitWorkReportCon.setCode(user.getCode());
            wpVisitInfo.setVisitWorkNo(user.getCode());
           // Integer visitCount = wpVisitInfoMapper.getWpVisitInfoList(wpVisitInfo);
            //接受回访
            wpVisitInfo.setAcceptVisit("1");

            //Integer sucessVisitCount = wpVisitInfoMapper.getWpVisitInfoList(wpVisitInfo);
            Integer sucessVisitCount = wpVisitInfoMapper.selectCountDistentVisitWpid2(wpVisitInfo);

            visitWorkReportCon.setSucessVisitCount(sucessVisitCount);
            //拒绝回访
            wpVisitInfo.setAcceptVisit("0");

            //Integer refuseVisitCount = wpVisitInfoMapper.getWpVisitInfoList(wpVisitInfo);
            Integer refuseVisitCount = wpVisitInfoMapper.selectCountDistentVisitWpid2(wpVisitInfo);

            visitWorkReportCon.setRefuseVisitCount(refuseVisitCount);
            //无需回访
            wpVisitInfo.setVisitResult("无需回访");
            wpVisitInfo.setAcceptVisit(null);
            Integer visitNoCount = wpVisitInfoMapper.getWpVisitInfoList2(wpVisitInfo);


            visitWorkReportCon.setVisitNoCount(visitNoCount);

            visitWorkReportCon.setVisitTotalCount(sucessVisitCount+refuseVisitCount);
            visitWorkReports.add(visitWorkReportCon);

        }

        tableDataInfo.setRows(visitWorkReports);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        return tableDataInfo;
    }
}
